package cn.edu.ctbu.myfirstweb.webapi;

import cn.edu.ctbu.myfirstweb.domain.SC;
import cn.edu.ctbu.myfirstweb.domain.SCID;
import cn.edu.ctbu.myfirstweb.helper.R;
import cn.edu.ctbu.myfirstweb.helper.RUtil;
import cn.edu.ctbu.myfirstweb.service.SCService;
import cn.edu.ctbu.myfirstweb.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 此api用于管理员端SC表管理，为管理员提供插入、删除、查询SC表的功能,
 * 可拓展功能：联合Course、Student、SC三表查询，并返回学生姓名，所选课程名称开课学期等信息。
 */

@RestController
@Slf4j
@RequestMapping("/webapi/sc")
public class SCRestController {

    @Autowired
    StudentService studentService;

    @Qualifier("SCImpService")
    @Autowired
    SCService scService;

    /**.**
    * @Description: 管理员端,SC插入方法
    * @Param:  request
    * @Return: R
    * @Author: HighJ
    * @Date Created in
    */

    @PostMapping("/insert")
    public R  insert(HttpServletRequest request){
        long sid = Integer.parseInt(request.getParameter("SID"));
        long cid = Integer.parseInt(request.getParameter("CID"));
        SCID  scid = new SCID(sid,cid);
        if(studentService.getByNum(sid)!=null && scService.getCourseByCid(cid)!=null && scService.getBySCID(scid) ==null) {
            SC sc = new SC();
            sc.setScid(scid);
            sc.setFlag(0);
            scService.insert(sc);
            return RUtil.success(sc);
        }
        return RUtil.error(520,"Error,this sc you want to insert is already exists.");
    }

    /**.**
    * @Description: 管理员端,SC删除方法
    * @Param: request
    * @Return: R
    * @Author: HighJ
    * @Date Created in
    */

    @DeleteMapping("/delete")
    public R delete(HttpServletRequest request){
        long sid = Integer.parseInt(request.getParameter("SID"));
        long cid = Integer.parseInt(request.getParameter("CID"));
        SCID  scid = new SCID(sid,cid);
        if(scService.getBySCID(scid) != null) {
            scService.delete(scid);
            return RUtil.success();
        }
        return RUtil.error(520,"Error,this SC you want to delete is null.");
    }

    /**.**
    * @Description: 通过cid查询SC表
    * @Param:  request
    * @Return: scList
    * @Author: HighJ
    * @Date Created in
    */

    @GetMapping("/querybycid")
    public R queryByCid(HttpServletRequest request){
        long cid = Integer.parseInt(request.getParameter("CID"));
        try {
            List<SC> scList = scService.queryByCID(cid);
            return RUtil.success(scList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RUtil.error(520,"Error,the cid that you query may not exists.");
    }
    /**.**
    * @Description: 通过scid查询SC表
    * @Param: request
    * @Return: sc
    * @Author: HighJ
    * @Date Created in
    */

    @GetMapping("/querybyscid")
    public R queryByScid(HttpServletRequest request){
        long sid = Integer.parseInt(request.getParameter("SID"));
        long cid = Integer.parseInt(request.getParameter("CID"));
        SCID  scid = new SCID(sid,cid);
        try {
            SC sc = scService.getBySCID(scid);
            return RUtil.success(sc);
        }catch (Exception e){
            e.printStackTrace();
        }
        return RUtil.error(520,"Error,the scid that you query may not exists.");
    }
/**.**
* @Description: 分页获取SC数据
* @Param:  page，saize
* @Return: scPage
* @Author: HighJ
* @Date Created in
*/

    @GetMapping("/getbypage")
    public R getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                       @RequestParam(value = "size", defaultValue = "3") Integer size) {
        /*新版本sort已不可实例化，只能通过sort.by来获取，同时此处的pageale对象已经变为protected的了，现在可通过Pageable.of方法构造*/
        Page<SC> scPage;
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "id"));
        scPage = scService.findAll(pageable);
        return RUtil.success(scPage);
    }

    @GetMapping("/getAll")
    public R getAll(){
        return RUtil.success(scService.finAll());
    }

}
